package com.example.springboot_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_admin.pojo.Orders;
import com.example.springboot_admin.service.IOrdersService;
import com.example.springboot_admin.vo.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author svblack
 * @since 2024-09-14
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    IOrdersService ordersService;

    @RequestMapping("list")
    IPage<Orders> list(@RequestParam(defaultValue = "1") int page,
                       @RequestParam(defaultValue = "5") int limit,
                       Integer order_id, Integer status) {
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        if (order_id != null) {
            wrapper.eq("order_id", order_id);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        return ordersService.page(new Page<>(page, limit), wrapper);
    }

    @RequestMapping("del")
    Integer del(int ordersid) {
        return ordersService.removeById(ordersid) ? 0 : 1;
    }

    @RequestMapping("doAdd")
    Integer doAdd(@RequestBody Orders orders) {
        return ordersService.save(orders) ? 0 : 1;
    }

    @RequestMapping("updateStatus")
    Integer updateStatus(@RequestParam Integer order_id, @RequestParam Integer status) {
        UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
        wrapper.set("status", status);
        wrapper.set("update_time", LocalDateTime.now());
        wrapper.eq("order_id", order_id);
        return ordersService.update(wrapper) ? 0 : 1;
    }

    // 配货方法
    @PutMapping("/checkDone")
    Integer checkDone(@RequestBody List<Integer> order_ids) {
        UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
        wrapper.in("order_id", order_ids);
        wrapper.set("status", "已配货"); // 这里假设"已配货"是配货后的状态标识符
        wrapper.set("update_time", LocalDateTime.now());
        return ordersService.update(wrapper) ? 0 : 1;
    }

    // 出库方法
    @PutMapping("/checkOut")
    Integer checkOut(@RequestBody List<Integer> order_ids) {
        UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
        wrapper.in("order_id", order_ids);
        wrapper.set("status", "已出库"); // 这里假设"已出库"是出库后的状态标识符
        wrapper.set("update_time", LocalDateTime.now());
        return ordersService.update(wrapper) ? 0 : 1;
    }

    // 关闭订单方法
    @PutMapping("/close")
    Integer close(@RequestBody List<Integer> order_ids) {
        UpdateWrapper<Orders> wrapper = new UpdateWrapper<>();
        wrapper.in("order_id", order_ids);
        wrapper.set("status", "已关闭"); // 这里假设"已关闭"是关闭后的状态标识符
        wrapper.set("update_time", LocalDateTime.now());
        return ordersService.update(wrapper) ? 0 : 1;
    }

    //获取订单详情
    @GetMapping("/{id}")
    OrderDetail getOrderDetail(@PathVariable("id") Integer order_id) {
        return ordersService.getOrderDetail(order_id);
    }
}